﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace AjaxControlFramework
{
    public class FileScriptReference : ScriptReference
    {
        //------// Properties \\--------------------------------------------\\
        private string _sourcePath = String.Empty;
        public virtual string SourcePath
        {
            get { return _sourcePath; }
            set
            {
                if (value == null)
                {
                    _sourcePath = String.Empty;
                }
                else
                {
                    _sourcePath = value;
                }
            }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public FileScriptReference(string name, string sourcePath)
            : base(name)
        {
            SourcePath = sourcePath;
        }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public override string GenerateScriptBlock()
        {
            string sourceUrl = String.Empty;

            Page currentPage = HttpContext.Current.CurrentHandler as Page;
            if (currentPage != null)
            {
                sourceUrl = currentPage.ResolveClientUrl(SourcePath);
            }

            StringBuilder scriptBlock = new StringBuilder(50 + sourceUrl.Length);
            scriptBlock.Append("<script type=\"text/javascript\" src=\"").Append(sourceUrl).Append("\"></script>");

            return scriptBlock.ToString();
        }
        //------\\ Methods //-----------------------------------------------//
    }
}